﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.BarterSystem.Barterables.ItemBarterable
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;

#nullable disable
namespace TaleWorlds.CampaignSystem.BarterSystem.Barterables
{
  public class ItemBarterable : Barterable
  {
    private readonly ItemRosterElement _itemElement;
    private readonly Hero _otherHero;
    private readonly PartyBase _otherParty;
    private readonly int _averageValueOfItemInNearbySettlements;

    public override string StringID => "item_barterable";

    public ItemBarterable(
      Hero ownerHero,
      Hero otherHero,
      PartyBase ownerParty,
      PartyBase otherParty,
      ItemRosterElement itemRosterElement,
      int averageValueOfItemInNearbySettlements)
      : base(ownerHero, ownerParty)
    {
      this._itemElement = itemRosterElement;
      this._averageValueOfItemInNearbySettlements = averageValueOfItemInNearbySettlements;
      this._otherParty = otherParty;
      this._otherHero = otherHero;
    }

    public ItemRosterElement ItemRosterElement => this._itemElement;

    public override int MaxAmount => this._itemElement.Amount;

    public override TextObject Name => this._itemElement.EquipmentElement.Item.Name;

    public int ItemValue => this._averageValueOfItemInNearbySettlements;

    public override int GetUnitValueForFaction(IFaction faction)
    {
      return MathF.Round(Campaign.Current.Models.BarterModel.GetBarterPenalty(faction, this, this._otherHero, this._otherParty).ResultNumber * (float) this.ItemValue);
    }

    public override void CheckBarterLink(Barterable parentLinkedBarterable)
    {
    }

    public override ImageIdentifier GetVisualIdentifier()
    {
      return new ImageIdentifier(this._itemElement.EquipmentElement.Item);
    }

    public override string GetEncyclopediaLink() => "";

    public override void Apply()
    {
      if (this._otherParty != null)
      {
        this.OriginalParty.ItemRoster.AddToCounts(this._itemElement.EquipmentElement, -this.CurrentAmount);
        this._otherParty.ItemRoster.AddToCounts(this._itemElement.EquipmentElement, this.CurrentAmount);
      }
      else
      {
        ItemRosterElement itemRosterElement = new ItemRosterElement(this._itemElement.EquipmentElement, this.CurrentAmount);
        GiveItemAction.ApplyForHeroes(this.OriginalOwner, this._otherHero, in itemRosterElement);
      }
    }

    internal static void AutoGeneratedStaticCollectObjectsItemBarterable(
      object o,
      List<object> collectedObjects)
    {
      ((Barterable) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }
  }
}
